﻿package model.drawPanel.entitys;

import java.io.Serializable;

import model.drawPanel.DrawPanelModel;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.widgets.Display;
import org.eclipse.wb.swt.SWTResourceManager;


public class Text extends Entity implements Serializable ,ITextChanger{
	/**
	 * 
	 */
	private static final long serialVersionUID = 101250106L;
	private  String content;
	private transient StyledText textInput;
	public Text(int id,Color color ,int x1, int y1,int x2,int y2 ,String content,StyledText textInput) {
		super(id,color, -1, -1, x1, y1, x2, y2);
		this.content = content;
		this.textInput = textInput;
	}
  
	public void setText(String msg) {
		content = msg;
	}
	public String  getText(){
		return content;
	}

@Override
public void draw(Display display, GC gc) {
	if(color ==null){
		super.initeColor();
	}
	
	if(this.isChoosed){
		if(textInput ==null){
			textInput =DrawPanelModel.getStyledText();
		}
		gc.setForeground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_NORMAL_SHADOW));
		gc.setLineWidth(3);
		gc.setLineStyle(SWT.LINE_SOLID);
		gc.drawOval(Math.min(x1, x2)-width/2-12, Math.min(y1, y2)-width/2-12, 12, 12);
		gc.drawOval(Math.max(x1, x2)+width/2, Math.min(y1, y2)-width/2-12, 12, 12);
		gc.drawOval(Math.min(x1, x2)-width/2-12, Math.max(y1, y2) +width/2, 12, 12);
		gc.drawOval(Math.max(x1, x2) + width/2, Math.max(y1, y2)+width/2, 12, 12);
		textInput.setBounds(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1- x2), Math.abs(y1- y2));
		if(content!= null){
			textInput.setText(content);
		}
		textInput.setBlockSelectionBounds(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x2-x1), Math.abs(2-y1));
		textInput.setVisible(true);
	}else{
	Font font = new Font(display,"Arial",14,SWT.BOLD | SWT.ITALIC);
	gc.setFont(font);
	gc.setForeground(color);
	if(content==null){
		return;
	}
	gc.drawText(content, Math.min(x1, x2), Math.min(y1, y2),true);
	}
}
@Override
public void  cancelChoosed(){
	if(!isChoosed){
		return;
	}
	System.out.println("textInput is cancel");
	isChoosed = false;
	if(textInput==null){
		textInput =DrawPanelModel.getStyledText();
	}
	textInput.setVisible(false);
	content = textInput.getText();
	textInput.setText("");
}

@Override
public String isFocused(int x, int y) {
	super.isFocused(x, y);
	if(isChoosed){
		if(distance(x1-width/2-6, y1-width/2-6,x,y) <= 6){
			return "zoomleftup";
		}
		if(distance(x1-width/2-6, y2 +width/2 +6,x,y) <= 6){
			return"zoomleftdown";
		}
		if(distance(x2+width/2 + 6, y1-width/2-6, x,y) <= 6 ){
			return"zoomrightup";
		}
		if(distance(x2 + width/2 +6, y2+width/2 +6,x,y) <= 6){
			return"zoomrightdown";
		}
	}
	if(x>= Math.min(x1, x2) && x <= Math.max(x1, x2) && y>=Math.min(y1, y2) && y<=Math.max(y1, y2)){
		return "entity";
	}
	return "noEnter";
}

@Override
public void changeText(int textRowNum, String change) {
	this.content = change;
	
}

@Override
public int getFocusedRowNum() {
	return 0;
}

@Override
public String getFocusedString() {
	// TODO Auto-generated method stub
	return content;
}

}